﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace GGJ.Sprites
{
    public class Fly
    {
        public enum type
        {
            civil = 0,
            soldier,
            general
        };

        private Texture2D sprites;
        private int tileSize = 64;
        public Rectangle destination;
        private int currentAnimationImage = 0;
        private double currentAnimationTime = 0.0f;
        public bool isWaiting;
        public FliesMovement causeOfDeath = FliesMovement.None;
        public bool isDead = false;
        private int idealX, idealY;

        public Fly(Texture2D sprites, int x, int y, bool isWaiting)
        {
            this.sprites = sprites;
            this.isWaiting = isWaiting;
            destination = new Rectangle(
                x,
                y,
                tileSize,
                tileSize);
            idealX = x;
            idealY = y;
        }

        public void setIdealXY(int x, int y)
        {
            idealX = x;
            idealY = y;
        }

        public Rectangle GetDestination()
        {
            Rectangle hitBox = new Rectangle(destination.X + 5, destination.Y + 5, destination.Width - 5,
                destination.Height - 5);
            return hitBox;
        }

        public bool isOutOfGame()
        {
            return ((destination.X + destination.Width < 0) || (destination.Y > 600));
        }

        public bool isInGame()
        {
            return ((destination.X >= 0) && (destination.X <= 800) && (destination.Y >= 0) && (destination.Y <= 600));
        }

        public bool isIdealInGame()
        {
            return ((idealX >= 0) && (idealX + tileSize <= 800) && (idealY >= 0) && (idealY + tileSize <= 600));
        }

        public bool CanMove(FliesMovement newMovement, float speed)
        {
            if (newMovement == FliesMovement.Up)
            {
                int newDestinationY = destination.Y - ((int)speed);
                if (newDestinationY < 0)
                    return false;
            }
            else if (newMovement == FliesMovement.Down)
            {
                int newDestinationY = destination.Y + ((int)speed);
                if (newDestinationY > (600 - tileSize))
                    return false;
            }

            return true;
        }

        public void Update(GameTime gameTime, FliesMovement newMovement, float speed,bool isSlowMotion)
        {
            if (newMovement == FliesMovement.Dead)
            {
                isDead = true;
                destination.X -= (int)speed;
  
            }
            else if (isWaiting)
            {
                destination.X -= (int)speed;
            }
            else
            {
                if (newMovement == FliesMovement.Up)
                {
                    destination.Y -= (int)speed;
                    idealY -= (int)speed;
                }
                else if (newMovement == FliesMovement.Down)
                {
                    destination.Y += (int)speed;
                    idealY += (int)speed;
                }
                else
                {
                    if (!isDead)
                    {
                        if (destination.Y > idealY)
                        {
                            destination.Y -= (int)speed;
                        }
                        if (destination.Y < idealY)
                        {
                            destination.Y += (int)speed;
                        }
                    }
                }
                if (!isDead)
                {
                    if (destination.X > idealX)
                    {
                        destination.X -= (int)speed;
                    }
                    if (destination.X < idealX)
                    {
                        destination.X += (int)speed;
                    }
                }
            }
            if (causeOfDeath == FliesMovement.Intoxicated)
            {
                if (destination.Y < 500)    
                    destination.Y += (int)speed;
            }
            else if (causeOfDeath == FliesMovement.Swatted)
            {
                destination.Y = 500;
            }            

            if (newMovement == FliesMovement.Up)
            {
                currentAnimationTime += gameTime.ElapsedGameTime.TotalSeconds;

                float time = (isSlowMotion ? 1.5f : 0.05f);

                if (currentAnimationTime > time)
                {
                    currentAnimationTime -= time;
                    currentAnimationImage++;

                    if (currentAnimationImage >= 6)
                        currentAnimationImage = 0;
                }
            }
            else if (newMovement == FliesMovement.None)
            {

                float time = (isSlowMotion ? 1.2f : 0.1f);

                currentAnimationTime += gameTime.ElapsedGameTime.TotalSeconds;
                if (currentAnimationTime > time)
                {
                    currentAnimationTime -= time;
                    currentAnimationImage++;

                    if (currentAnimationImage >= 6)
                        currentAnimationImage = 0;
                }
            }
            else if (newMovement == FliesMovement.Down)
            {
                currentAnimationTime = 0;
                currentAnimationImage = 1;
            }
        }

        public void Draw(SpriteBatch spriteBatch,Fly.type type)
        {
            Rectangle source = new Rectangle();


            if (causeOfDeath == FliesMovement.None)
            {
                if (type==type.civil)
                {
                    source = new Rectangle(
                     currentAnimationImage * tileSize,
                     0 * tileSize,
                     tileSize,
                     tileSize);
                }
                else if (type == type.general)
                {
                    source = new Rectangle(
                     currentAnimationImage * tileSize,
                     2 * tileSize,
                     tileSize,
                     tileSize);

                }
                else if (type==type.soldier)
                {
                    source = new Rectangle(
                     currentAnimationImage * tileSize,
                     3 * tileSize,
                     tileSize,
                     tileSize);

                }
             
            }
            else if (causeOfDeath == FliesMovement.Stuck)
            {
                source = new Rectangle(
                0 * tileSize,
                1 * tileSize,
                tileSize,
                tileSize);
            }
            else if (causeOfDeath == FliesMovement.Intoxicated)
            {
                source = new Rectangle(
                2 * tileSize,
                1 * tileSize,
                tileSize,
                tileSize);
            }
            else if (causeOfDeath == FliesMovement.Swatted)
            {
                source = new Rectangle(
                1 * tileSize,
                1 * tileSize,
                tileSize,
                tileSize);
            }
          

            spriteBatch.Draw(sprites, destination, source, Color.White);
        }
    }
}
